/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yuxl.admin.common.core.config;

import lombok.val;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author lengleng
 * @date 2019/2/1 Redis 配置类
 */
@EnableCaching
@Configuration
public class RedisTemplateConfiguration {

	@Bean
	public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
		RedisSerializationContext<String, Object> build = RedisSerializationContext.<String, Object>newSerializationContext(RedisSerializer.string())
				.value(new GenericJackson2JsonRedisSerializer()).build();
		return new ReactiveRedisTemplate<>(factory, build);
	}

	@Bean
	public ReactiveHashOperations<String, String, Object> hashOperations(ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
		return reactiveRedisTemplate.opsForHash();
	}

	@Bean
	public ReactiveValueOperations<String, String> valueOperations(ReactiveRedisTemplate<String, String> reactiveRedisTemplate) {
		return reactiveRedisTemplate.opsForValue();
	}

	@Bean
	public ReactiveListOperations<String, Object> listOperations(ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
		return reactiveRedisTemplate.opsForList();
	}

	@Bean
	public ReactiveSetOperations<String, Object> setOperations(ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
		return reactiveRedisTemplate.opsForSet();
	}

	@Bean
	public ReactiveZSetOperations<String, Object> zSetOperations(ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
		return reactiveRedisTemplate.opsForZSet();
	}

}
